package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.BaseAction;
import com.tebiecloud.base.server.service.BaseActionService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统资源-功能操作 前端控制器
 * @author tebie
 * @date 2019-08-08
 */
@RestController
@RequestMapping("baseaction")
public class BaseActionController {

    @Autowired
    private BaseActionService baseActionService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseAction>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseAction query = pageParams.mapToObject(BaseAction.class);
        QueryWrapper<BaseAction> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseActionService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseAction> get(@RequestParam("id") Long id) {
        BaseAction entity = baseActionService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param actionCode 资源编码,
     * @param actionName 资源名称,
     * @param actionDesc 资源描述,
     * @param menuId 资源父节点,
     * @param priority 优先级 越小越靠前,
     * @param status 状态:0-无效 1-有效,
     * @param isPersist 保留数据0-否 1-是 不允许删除,
     * @param serviceId 服务名称
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "actionCode", required = false) String actionCode,
        @RequestParam(value = "actionName", required = false) String actionName,
        @RequestParam(value = "actionDesc", required = false) String actionDesc,
        @RequestParam(value = "menuId", required = false) Long menuId,
        @RequestParam(value = "priority", required = false) Integer priority,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "isPersist", required = false) Integer isPersist,
        @RequestParam(value = "serviceId", required = false) String serviceId
    ) {
        BaseAction entity = new BaseAction();
        entity.setActionCode(actionCode);
        entity.setActionName(actionName);
        entity.setActionDesc(actionDesc);
        entity.setMenuId(menuId);
        entity.setPriority(priority);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setServiceId(serviceId);
        baseActionService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param actionCode 资源编码
     * @param actionName 资源名称
     * @param actionDesc 资源描述
     * @param menuId 资源父节点
     * @param priority 优先级 越小越靠前
     * @param status 状态:0-无效 1-有效
     * @param isPersist 保留数据0-否 1-是 不允许删除
     * @param serviceId 服务名称
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "actionId", required = false) Long actionId,
            @RequestParam(value = "actionCode", required = false) String actionCode,
            @RequestParam(value = "actionName", required = false) String actionName,
            @RequestParam(value = "actionDesc", required = false) String actionDesc,
            @RequestParam(value = "menuId", required = false) Long menuId,
            @RequestParam(value = "priority", required = false) Integer priority,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "isPersist", required = false) Integer isPersist,
            @RequestParam(value = "serviceId", required = false) String serviceId
    ) {
        BaseAction entity = new BaseAction();
        entity.setActionId(actionId);
        entity.setActionCode(actionCode);
        entity.setActionName(actionName);
        entity.setActionDesc(actionDesc);
        entity.setMenuId(menuId);
        entity.setPriority(priority);
        entity.setStatus(status);
        entity.setIsPersist(isPersist);
        entity.setServiceId(serviceId);
        baseActionService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            baseActionService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            baseActionService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
